package com.ssm.lzh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.hj.page.Page;
import com.ssm.lzh.service.BookingService;
import com.ssm.lzh.vo.BookingView;
import com.ssm.mapper.BookingMapper;
import com.ssm.pojo.Booking;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Service
public class BookingServiceImpl implements BookingService {

    @Resource
    private BookingMapper bookingMapper;

    @Override
    public PageInfo<BookingView> queryAllBooking(Page<BookingView> page) {
        PageHelper.startPage(page.getPageNum(),page.getPageSize());

        return new PageInfo<BookingView>(this.bookingMapper.queryAllBooking(page.getSearchObj()));
    }

    @Override
    public int confirmBooking(Booking booking) {
        System.out.println(booking);
        if(this.bookingMapper.confirmBooking(booking.getRid())>0){
            this.bookingMapper.insertTjsc(booking);
//            register(booking.getPhone());
            return 1;
        }
        return 0;
    }

    public int register(String phone){
        System.out.println(phone);
        //在短信宝注册的用户名
        String testUsername = "huangchaod";
        //在短信宝注册的密码
        String testPassword = "hc19976783043";
        String testPhone = phone;
        //验证码
        int VerificationCode= (int)((Math.random()*9+1)*1000);
        System.out.println(VerificationCode);
        // 注意测试时，也请带上公司简称或网站签名，发送正规内容短信。千万不要发送无意义的内容：例如 测一下、您好。否则可能会收不到
        String testContent = "【杰少健康】您的验证码是"+VerificationCode+",５分钟内有效。若非本人操作请忽略此消息。";

        String httpUrl = "http://api.smsbao.com/sms";

        StringBuffer httpArg = new StringBuffer();
        httpArg.append("u=").append(testUsername).append("&");
        httpArg.append("p=").append(md5(testPassword)).append("&");
        httpArg.append("m=").append(testPhone).append("&");
        httpArg.append("c=").append(encodeUrlString(testContent, "UTF-8"));

        String result = request(httpUrl, httpArg.toString());
        System.out.println(result);
        return VerificationCode;

    }

    public static String encodeUrlString(String str, String charset) {
        String strret = null;
        if (str == null) {
            return str;
        }
        try {
            strret = java.net.URLEncoder.encode(str, charset);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return strret;
    }

    public static String request(String httpUrl, String httpArg) {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "?" + httpArg;

        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = reader.readLine();
            if (strRead != null) {
                sbf.append(strRead);
                while ((strRead = reader.readLine()) != null) {
                    sbf.append("\n");
                    sbf.append(strRead);
                }
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String md5(String plainText) {
        StringBuffer buf = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();
            int i;
            buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i));
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return buf.toString();
    }


    //取消预约
    @Override
    public int cancelBooking(int rid) {
        return this.bookingMapper.cancelBooking(rid);
    }

    @Override
    public int addBooking(Booking booking) {

        System.out.println(booking);
        return this.bookingMapper.addBooking(booking);
    }
}
